const viewModules = import.meta.glob('../views/**/*.vue')
const layoutModules = import.meta.glob('../layouts/**/*.vue')
const pluginModules = import.meta.glob('../plugin/**/*.vue')


export const asyncRouterHandle = (asyncRouter) => {
	asyncRouter.forEach(item => {
		if (item.component) {
			if (typeof (item.component) == "string") {
				if (item.component.split('/')[0] === 'views') {
					item.component = dynamicImport(viewModules, item.component)
				} else if (item.component.split('/')[0] === 'layouts') {
					item.component = dynamicImport(layoutModules, item.component)
				} else if (item.component.split('/')[0] === 'plugin') {
					item.component = dynamicImport(pluginModules, item.component)
				}
			}
			
		} else {
			delete item['component']
		}
		if (item.children) {
			asyncRouterHandle(item.children)
		}
	})
	return asyncRouter
}

function dynamicImport(
	dynamicViewsModules,
	component
) {
	const keys = Object.keys(dynamicViewsModules)
	const matchKeys = keys.filter((key) => {
		const k = key.replace('../', '')
		return k === component
	})
	const matchKey = matchKeys[0]

	return dynamicViewsModules[matchKey]
}


